package com.huawang.mdesign.model.service.model.core.display.icon;

import com.huawang.business.dto.BlockPropertyData;
import com.huawang.business.dto.graph.ModelInfo;
import com.huawang.business.service.model.core.InternalUmlService;
import com.huawang.business.service.model.core.display.executor.show.MethodLibrary;
import com.huawang.business.service.model.core.display.executor.show.ShowPropertyStatus;
import com.huawang.business.service.model.core.display.icon.FetchSpecificModelInfoValueInternalUmlService;
import com.huawang.core.definition.mof.UMLDefinitionConstants;
import com.huawang.core.definition.mof.instances.MetaClassInstance;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class UAFQuerySpecialModelIcon extends com.huawang.business.service.model.core.display.icon.QuerySpecialModelIcon {
    private FetchSpecificModelInfoValueInternalUmlService fetchSpecificModelInfoValue;
    private List<MetaClassInstance> stereotypes;
    private boolean hasRunE = false;
    private boolean hasRunF = false;
    private boolean hasRunG = false;
    private boolean hasRunH = false;

    public UAFQuerySpecialModelIcon(@NotNull MetaClassInstance owningInstance, @NotNull ShowPropertyStatus proceedingComputedStatus, @NotNull MethodLibrary methodFunction, @NotNull InternalUmlService internalUmlService, @NotNull String projectId, ModelInfo modelInfo) {
        super(owningInstance, proceedingComputedStatus, methodFunction, internalUmlService, projectId, modelInfo);
    }

    @Override
    @Nullable
    protected String handleProperty(String projectId, ModelInfo modelInfo) {
        BlockPropertyData<List<MetaClassInstance>> appliedStereotypeData = queryListInstanceValueOrSaveBlock(this.owningInstance,
                UMLDefinitionConstants.Element_appliedStereotype);
        if (appliedStereotypeData.isBlock()) {
            return null;
        }
        this.stereotypes = appliedStereotypeData.getValue().stream().filter(it ->
                !it.getId().contains("Property")).collect(Collectors.toList());

        if(CollectionUtils.isNotEmpty(stereotypes)) {
            fetchSpecificModelInfoValue = Objects.isNull(fetchSpecificModelInfoValue) ?
                    new FetchSpecificModelInfoValueInternalUmlService(
                            stereotypes.get(0), this.proceedingComputedStatus,
                            this.methodFunction, this.internalUmlService, projectId
                    ) : fetchSpecificModelInfoValue;
            if (executeModelExecutor(fetchSpecificModelInfoValue)) {
                return null;
            }
            String key = fetchSpecificModelInfoValue.getValue();
            ModelInfo stereotypeInfo = managerConfig.getModelInfoConfig().get(key);
            if (Objects.nonNull(stereotypeInfo) && internalUmlService.checkInstanceType(projectId, this.owningInstance, UMLDefinitionConstants.UAF_ResourceRole)) {
                BlockPropertyData<List<MetaClassInstance>> resourceRoleKinds = queryListInstanceValueOrSaveBlock(this.owningInstance,
                        UMLDefinitionConstants.UAF_ResourceRole_roleKind);
                if (resourceRoleKinds.isBlock()) {
                    return null;
                }
                if (CollectionUtils.isNotEmpty(resourceRoleKinds.getValue())) {
                    return getPropertyIcon(resourceRoleKinds.getValue().get(0).getUUID());
                } else {
                    return stereotypeInfo.getIcon();
                }
            }
        }
        return null;
    }

    /**
     * 属性图标
     *
     */
    @NotNull
    private static String getPropertyIcon(String roleKindId) {
        String[] roleKindNames = roleKindId.split("::");
        if(roleKindNames.length > 2) {
            return "statics/arch/icon/" + roleKindNames[roleKindNames.length-2] + roleKindNames[roleKindNames.length-1] + ".svg";
        } else {
            return "statics/images/sysml/Property.svg";
        }
    }
}
